﻿using Basic.FormDAL;
using Basic.FormModel.DB;
using Basic.FormRemoteModel;
using Basic.FormRemoteModel.Page.Model;
using WeDonekRpc.Helper;
using WeDonekRpc.Model;

namespace Basic.FormCollect.lmpl
{
    internal class CustomPageCollect : ICustomPageCollect
    {
        private readonly ICustomPageDAL _BasicDAL;

        public CustomPageCollect ( ICustomPageDAL basicDAL )
        {
            this._BasicDAL = basicDAL;
        }
        public long Add ( PageAdd data )
        {
            if ( this._BasicDAL.IsExists(c => c.CompanyId == data.CompanyId && c.PageClassify == data.PageClassify && c.PageName == data.PageName) )
            {
                throw new ErrorException("form.page.name.repeat");
            }
            return this._BasicDAL.Add(data);
        }

        public void Delete ( DBCustomPage db )
        {
            this._BasicDAL.Delete(db.Id);
        }

        public bool Disable ( DBCustomPage page )
        {
            if ( page.Status == FormPageStatus.停用 )
            {
                return false;
            }
            page.Status = FormPageStatus.停用;
            return this._BasicDAL.Update(page, new string[] { "Status" });
        }

        public bool Enable ( DBCustomPage page )
        {
            if ( page.Status == FormPageStatus.启用 )
            {
                return false;
            }
            page.Status = FormPageStatus.启用;
            return this._BasicDAL.Update(page, new string[] { "Status" });
        }

        public Result Get<Result> ( long id ) where Result : class, new()
        {
            Result res = this._BasicDAL.Get<Result>(c => c.Id == id);
            if ( res == null )
            {
                throw new ErrorException("form.custom.page.not.find");
            }
            return res;
        }
        public Result[] Query<Result> ( PageQueryParam query, IBasicPage paging, out int count ) where Result : class
        {
            return this._BasicDAL.Query<Result>(query, paging, out count);
        }
        public bool Set ( DBCustomPage page, PageSet set )
        {
            if ( ( page.PageName != set.PageName || page.PageClassify != set.PageClassify ) && this._BasicDAL.IsExists(c => c.CompanyId == page.CompanyId && c.PageClassify == set.PageClassify && c.PageName == set.PageName) )
            {
                return false;
            }
            return this._BasicDAL.Update(page, set);
        }
    }
}
